package com.zmzncs.lmtc.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.pojo.ResultHead;
import com.zmzncs.lmtc.common.pojo.tengda.CarForm;
import com.zmzncs.lmtc.common.pojo.tengda.TengdaForm;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class TengdaUtil {

    //  实时订单
    public static String queryTengdaCurrOrder;
    @Value("${zhongmeng.request-url.queryTengdaCurrOrder}")
    public void setQueryTengdaCurrOrder(String queryTengdaCurrOrder) {
        this.queryTengdaCurrOrder = queryTengdaCurrOrder;
    }

    //  添加月租
    public static String buyMonthCard;
    @Value("${zhongmeng.request-url.buyMonthCard}")
    public void setBuyMonthCard(String buyMonthCard) {
        this.buyMonthCard = buyMonthCard;
    }

    //  续月租
    public static String monthCardRenewal;
    @Value("${zhongmeng.request-url.monthCardRenewal}")
    public void setMonthCardRenewal(String monthCardRenewal) {
        this.monthCardRenewal = monthCardRenewal;
    }

    //  车牌月租
    public static String monthCard;
    @Value("${zhongmeng.request-url.monthCard}")
    public void setMonthCard(String monthCard) {
        this.monthCard = monthCard;
    }

    //  冻结月租车
    public static String freeze;
    @Value("${zhongmeng.request-url.freeze}")
    public void setFreeze(String freeze) {
        this.freeze = freeze;
    }

    //  解冻月租车
    public static String unfreeze;
    @Value("${zhongmeng.request-url.unfreeze}")
    public void setUnfreeze(String unfreeze) {
        this.unfreeze = unfreeze;
    }

    /**
     *冻结月租车
     */
    public static TengdaForm freeze(String bolianParkId, String parkMonthId){
        Map<String, Object> paramsa = new HashMap<>();
        paramsa.put("bolianParkId", bolianParkId); //订单记录号
        paramsa.put("parkMonthId", parkMonthId); //车场月租车ID
        log.info("腾达冻结月租车请求数据：{}", paramsa);

        String resultStr = HttpUtil.getIntance().post(freeze, paramsa);
        log.info("腾达冻结月租车响应数据：{}", resultStr);

        TengdaForm tengdaForm = JSONObject.parseObject(resultStr, TengdaForm.class);

        return tengdaForm;
    }

    /**
     *解冻月租车
     */
    public static TengdaForm unfreeze(String bolianParkId, String parkMonthId){
        Map<String, Object> paramsa = new HashMap<>();
        paramsa.put("bolianParkId", bolianParkId); //订单记录号
        paramsa.put("parkMonthId", parkMonthId); //车场月租车ID
        log.info("腾达解冻月租车请求数据：{}", paramsa);

        String resultStr = HttpUtil.getIntance().post(unfreeze, paramsa);
        log.info("腾达解冻月租车响应数据：{}", resultStr);

        TengdaForm tengdaForm = JSONObject.parseObject(resultStr, TengdaForm.class);

        return tengdaForm;
    }

    /**
     *实时订单
     */
    public static TengdaForm queryCurrOrder(String bolianParkId, String carNumber){
        Map<String, Object> paramsa = new HashMap<>();
        paramsa.put("bolianParkId", bolianParkId); //订单记录号
        paramsa.put("carNumber", carNumber); //空闲车位数
        log.info("腾达实时订单请求数据：{}", paramsa);

        String resultStr = HttpUtil.getIntance().get(queryTengdaCurrOrder, paramsa);
        log.info("腾达实时订单响应数据：{}", resultStr);

        TengdaForm tengdaForm = JSONObject.parseObject(resultStr, TengdaForm.class);

        return tengdaForm;
    }

    /**
     * 添加月租
     */
    public static TengdaForm buyMonthCard(String bolianParkId, String carNum, Integer carTypeCode, BigDecimal carMMoney, Date timeStart, Date timeEnd) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("bolianParkId", bolianParkId);//停车场ID
        params.put("carNum", carNum);//车牌号码
        params.put("carTypeCode", carTypeCode);//车牌号码
        params.put("amount", carMMoney);//收费金额
        params.put("timeStart", DateTimeUtil.convertYMDHMS(timeStart));//月租开始时间 yyyy-MM-dd HH:mm:ss
        params.put("timeEnd", DateTimeUtil.convertYMDHMS(timeEnd));//月租结束时间 yyyy-MM-dd HH:mm:ss
        log.info("腾达添加月租请求数据：" + params);
        String resultStr = HttpUtil.getIntance().post(buyMonthCard, params);
        log.info("腾达添加月租响应结果：" + resultStr);

        TengdaForm tengdaForm = JSONObject.parseObject(resultStr, TengdaForm.class);

        return tengdaForm;
    }

    /**
     * 月卡续期
     */
    public static TengdaForm monthRenewal(String parkMonthId, String bolianParkId, String carNum, Integer carTypeCode, BigDecimal carMMoney, Date timeStart, Date timeEnd) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("parkMonthId", parkMonthId);
        params.put("bolianParkId", bolianParkId);//停车场ID
        params.put("carNum", carNum);//车牌号码
        params.put("carTypeCode", carTypeCode);//车辆类型编码
        params.put("amount", carMMoney);//收费金额
        params.put("timeStart", DateTimeUtil.convertYMDHMS(timeStart));//月租开始时间 yyyy-MM-dd HH:mm:ss
        params.put("timeEnd", DateTimeUtil.convertYMDHMS(timeEnd));//月租结束时间 yyyy-MM-dd HH:mm:ss
//        String jsonParams =  JSON.toJSONString(params);
        log.info("腾达月卡续期请求数据：" + params);
        String resultStr = HttpUtil.getIntance().post(monthCardRenewal, params);
        log.info("腾达月卡续期响应结果：" + resultStr);

        TengdaForm tengdaForm = JSONObject.parseObject(resultStr, TengdaForm.class);
//        CarForm carForm = JSONObject.toJavaObject(tengdaForm.getDataItems().get(0), CarForm.class);

        return tengdaForm;
    }

    /**
     * 车牌月租
     */
    public static TengdaForm monthCard(String bolianParkId, String carNum, Integer carTypeCode) {
        Map<String, Object> paramsa = new HashMap<>();
        paramsa.put("bolianParkId", bolianParkId); //   泊链车场id
        paramsa.put("carNum", carNum); //    车牌
        paramsa.put("carTypeCode", carTypeCode); //    月租编号
        log.info("腾达车牌月租请求数据：{}", paramsa);

        String resultStr = HttpUtil.getIntance().get(monthCard, paramsa);
        log.info("腾达车牌月租响应结果：" + resultStr);

        TengdaForm tengdaForm = JSONObject.parseObject(resultStr, TengdaForm.class);

        return tengdaForm;
    }

}
